package cn.edu.sdu.java.server.controllers;
import cn.edu.sdu.java.server.models.Lecture;
import cn.edu.sdu.java.server.models.Student;
import cn.edu.sdu.java.server.payload.request.DataRequest;
import cn.edu.sdu.java.server.payload.response.DataResponse;
import cn.edu.sdu.java.server.payload.response.OptionItem;
import cn.edu.sdu.java.server.payload.response.OptionItemList;
import cn.edu.sdu.java.server.repositorys.LectureRepository;
import cn.edu.sdu.java.server.repositorys.StudentRepository;
import cn.edu.sdu.java.server.util.CommonMethod;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/api/lecture")
public class LectureController {
    @Autowired
    private StudentRepository studentRepository;
    @Autowired
    private LectureRepository lectureRepository;
    @PostMapping("/getStudentItemOptionList")
    public OptionItemList getStudentItemOptionList(@Valid @RequestBody DataRequest dataRequest) {
        List<Student> sList = studentRepository.findStudentListByNumName("");  //数据库查询操作
        OptionItem item;
        List<OptionItem> itemList = new ArrayList();
        for (Student s : sList) {
            itemList.add(new OptionItem( s.getStudentId(),s.getStudentId()+"", s.getPerson().getNum()+"-"+s.getPerson().getName()));
        }
        return new OptionItemList(0, itemList);}
        @PostMapping("/getLectureList")
        public DataResponse getCourseAttendanceList(@Valid @RequestBody DataRequest dataRequest){
            Integer studentId = dataRequest.getInteger("personId");
            if(studentId == null)
                studentId = 0;

            List<Lecture> cList=lectureRepository.findByStudentStudentId(studentId);
            List dataList=new ArrayList<>();
            Map m;
            for(Lecture s:cList){
                if (s != null) {
                    m = new HashMap();
                    m.put("lectureId", s.getLectureId()+"");
                    m.put("studentId",s.getStudent().getStudentId()+"");
                    m.put("speaker",s.getSpeaker());
                    m.put("topic",s.getTopic());
                    m.put("student",s.getStudent());
                    m.put("location",s.getLocation());
                    m.put("studentNum",s.getStudent().getPerson().getNum());
                    m.put("studentName",s.getStudent().getPerson().getName());
                    m.put("time",s.getTime());
                    m.put("type",s.getType());
                    dataList.add(m);
                }


            }
            return CommonMethod.getReturnData(dataList);
        }
        @PostMapping("/newLecture")
        public DataResponse newLecture(@Valid@RequestBody DataRequest dataRequest){
            Integer studentId= dataRequest.getInteger("personId");
            Optional<Student> op=studentRepository.findById(studentId);
            if(!op.isPresent() ){
                return CommonMethod.getReturnMessageError("该学生不存在");}
            String newSpeaker= dataRequest.getString("newSpeaker");
            String newLocation=dataRequest.getString("newLocation");
            String newTime=dataRequest.getString("newTime");
            String newTopic= dataRequest.getString("newTopic");
            String newType= dataRequest.getString("newType");
            Lecture h=new Lecture();
            h.setStudent(op.get());
            h.setTime(newTime);
            h.setSpeaker(newSpeaker);
            h.setTopic(newTopic);
            h.setLocation(newLocation);
            h.setType(newType);
            lectureRepository.save(h);
            return CommonMethod.getReturnMessageOK();
        }
        @PostMapping("/deleteLecture")
    public DataResponse deleteLecture(@Valid@RequestBody DataRequest dataRequest) {
        Integer lectureId= dataRequest.getInteger("lectureId");
        if(lectureId==null){
            return CommonMethod.getReturnMessageError("传输错误，找不到数据");
        }
        Optional<Lecture> op=lectureRepository.findById(lectureId);
        if(!op.isPresent()){
            return CommonMethod.getReturnMessageError("找不到这条记录！");

        }
            Lecture h= op.get();
            lectureRepository.delete(h);
            return CommonMethod.getReturnMessageOK();
        }
        public void localLectureDelete(Integer studentId){
        List<Lecture> list=lectureRepository.findByStudentId(studentId);
        lectureRepository.deleteAll(list);
    }
    @PostMapping("/lectureEditSave")
    public DataResponse lectureEditSave(@Valid@RequestBody DataRequest dataRequest) {
        Integer lectureId = dataRequest.getInteger("lectureId");
        if (lectureId == null) {
            return CommonMethod.getReturnMessageError("传输错误，找不到数据");
        }
        Optional<Lecture> op = lectureRepository.findById(lectureId);
        if (!op.isPresent()) {
            return CommonMethod.getReturnMessageError("找不到这条记录！");

        }
        Lecture h = op.get();
        String topic = dataRequest.getString("topic");
        String time = dataRequest.getString("time");
        String speaker = dataRequest.getString("speaker");
        String type = dataRequest.getString("type");
        String location = dataRequest.getString("location");
        h.setType(type);
        h.setTime(time);
        h.setTopic(topic);
        h.setSpeaker(speaker);
        h.setLocation(location);
        lectureRepository.save(h);
        return CommonMethod.getReturnMessageOK();
    }
    @PostMapping("/getLectureListByNumName")
    public DataResponse getLectureList(@Valid @RequestBody DataRequest dataRequest){
        String num= dataRequest.getString("num");
        if(num==null){
            num="";
        }
        String name= dataRequest.getString("name");
        if(name==null){
            name="";
        }
        String location= dataRequest.getString("location");
        if(location==null){
            location="";
        }
        List<Lecture> lectureList=lectureRepository.findByNumNameLocation(num,name,location);
        List dataList=new ArrayList<>();
        Map m;
        for(Lecture s:lectureList){
            if (s != null) {
                m = new HashMap();
                m.put("lectureId", s.getLectureId()+"");
                m.put("studentId",s.getStudent().getStudentId()+"");
                m.put("speaker",s.getSpeaker());
                m.put("location",s.getLocation());
                m.put("student",s.getStudent());
                m.put("topic",s.getTopic());
                m.put("studentNum",s.getStudent().getPerson().getNum());
                m.put("studentName",s.getStudent().getPerson().getName());
                m.put("time",s.getTime());
                m.put("type",s.getType());
                dataList.add(m);
            }
        }
        return CommonMethod.getReturnData(dataList);
    }
}
